{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import joblib\n",
    "import warnings\n",
    "import os\n",
    "from sklearn.preprocessing import StandardScaler\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "use_cols = joblib.load('./use_cols')\n",
    "scaler = joblib.load('./scaler')\n",
    "pca = joblib.load('./model/pca_1XX')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def load_test(train_no=1, use_cols=[]):\n",
    "    input_dir = '../../2.2preprocess_test_data/concats_cut/'\n",
    "    train = joblib.load(input_dir+'concat_0%d.lz4'%train_no) \n",
    "    x_train = train[use_cols]\n",
    "    CLI = train['CLI']\n",
    "    CL = train['CL']\n",
    "    return x_train, CLI, CL\n",
    "\n",
    "def standard_scale_new(data,scaler):\n",
    "    cols = data.columns\n",
    "    idx = data.index\n",
    "    data_s = scaler.transform(data.values)\n",
    "    return pd.DataFrame(data=data_s,columns=cols,index=idx)\n",
    "\n",
    "def cal_T2(data,pca):\n",
    "    T2=[]\n",
    "    projected_data = pca.transform(data)\n",
    "    eigenvalue_matrix = np.mat(np.diag(pca.singular_values_ ))\n",
    "    for x in projected_data:\n",
    "        tmp = np.mat(x)*np.linalg.inv(eigenvalue_matrix)*np.mat(x).T\n",
    "        T2.append(tmp[0,0])\n",
    "    return T2\n",
    "\n",
    "def write_files(test_no, use_cols, scaler, pca):\n",
    "    x_test, CLI, CL = load_test(test_no, use_cols)\n",
    "    data_stand = standard_scale_new(x_test,scaler)\n",
    "    T2 = cal_T2(data_stand,pca)\n",
    "    T2 = pd.DataFrame(data=T2,columns=['PCA_T2'], index=data_stand.index)\n",
    "    T2['CLI'] = CLI\n",
    "    T2['CL'] = CL\n",
    "    if not os.path.exists('./test_HA'):\n",
    "        os.mkdir('./test_HA')\n",
    "    joblib.dump(T2, './test_HA/test_HA_0%d.lz4'%test_no, compress='lz4')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "write_files(1, use_cols, scaler, pca)\n",
    "write_files(2, use_cols, scaler, pca)\n",
    "write_files(3, use_cols, scaler, pca)\n",
    "write_files(4, use_cols, scaler, pca)\n",
    "write_files(5, use_cols, scaler, pca)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
